package com.gxa.service.impl;

import com.gxa.common.PaginationResult;
import com.gxa.mapper.*;
import com.gxa.pojo.*;
import com.gxa.service.ProductCodeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: zxy
 * Date: 2020/10/31 10:23
 * Description:
 * Version: V1.0
 */
@Service
public class ProductCodeServiceImpl implements ProductCodeService{

    @Autowired(required = false)
    private ProductCodeMapper productCodeMapper;

    @Autowired(required = false)
    private CoditAttribMapper attribMapper;

    @Autowired(required = false)
    private CoditInforMapper coditInforMapper;

    @Autowired(required = false)
    private CostPriceMapper costPriceMapper;

    @Autowired(required = false)
    private CostSellingMapper costSellingMapper;

    @Autowired(required = false)
    private LogRecordMapper logRecordMapper;

    @Autowired(required = false)
    private CoditTypMapper coditTypMapper;

    @Autowired(required = false)
    private BillCodeMapper billCodeMapper;
    @Override
    public PaginationResult queryProCode(int currentPage, int pageSize) {
        PaginationResult paginationResult = new PaginationResult();
        //当前页码数
        paginationResult.setCurrentPage(currentPage);
        //每页显示条数
        paginationResult.setPageSize(pageSize);
        //总条数
        int totalSize = getTotalSizePro();
        //总页数
        int totalPage = totalSize / pageSize;
        if (totalSize % pageSize != 0) {
            totalPage += 1;
        }
        paginationResult.setTotalPage(totalPage);
        paginationResult.setTotalSize(totalSize);
        int param1 = (currentPage - 1) * pageSize;
        int param2 = pageSize;
        List<BillCode> billCodeList = productCodeMapper.queryProductCode(param1, param2);
        System.out.println("billCodeList=" + billCodeList);
        for (BillCode billCode : billCodeList) {
            billCode.setCoditInfor(coditInforMapper.querycoditInfor(billCode.getCoditInforId()));
            billCode.setCostPrice(costPriceMapper.querycostPrice(billCode.getCostPriceId()));
            billCode.setCostSelling(costSellingMapper.querycostSelling(billCode.getCostPriceGoodId()));
            billCode.setCoditTyp(coditTypMapper.querycoditTyp(billCode.getCoditTypId()));
//            billCode.setBillCodeId();
            System.out.println("billCode" + billCode);
        }
        System.out.println("billCodeList=" + billCodeList);

        paginationResult.setData(billCodeList);
        return paginationResult;
    }

    private int getTotalSizePro() {
        return productCodeMapper.getTotalSizePro();
    }

    @Override
    public int addProCode(BillCode billCode) {
        CoditAttrib coditAttrib = billCode.getCoditAttrib();
        CoditInfor coditInfor = billCode.getCoditInfor();
        CostPrice costPrice = billCode.getCostPrice();
        CostSelling costSelling = billCode.getCostSelling();
        LogRecord logRecord = billCode.getLogRecord();
        CoditTyp coditTyp = billCode.getCoditTyp();
        int result = attribMapper.addCoditAttribByPro(coditAttrib);
        Integer coditAttribId = coditAttrib.getCoditAttribId();
        int result1 = coditInforMapper.addCoditInforByPro(coditInfor);
        Integer coditInforId = coditInfor.getCoditInforId();
        int result2 = costPriceMapper.addCostPriceByPro(costPrice);
        Integer costPriceId = costPrice.getCostPriceId();
        int result3 = costSellingMapper.addCostSellingByPro(costSelling);
        Integer costPriceGoodId = costSelling.getCostPriceGoodId();
        int result4 = logRecordMapper.addLogRecordByPro(logRecord);
        Integer logRecordId = logRecord.getLogRecordId();
        int result5 = coditTypMapper.addCoditTypeMapperByPro(coditTyp);
        Integer coditTypId = coditTyp.getCoditTypId();
        int Result=billCodeMapper.addBillCode(coditAttribId,coditInforId,costPriceId,costPriceGoodId,logRecordId,coditTypId);
        return Result;
    }

    @Override
    public int delProCode(Integer billCodeId) {
        return productCodeMapper.delProCode(billCodeId);
    }

    @Override
    public List<List<String>> queryProCodeSelect() {
        List<String> coditTypSelect = productCodeMapper.coditTypSelect();
        List<String> coditInforSelect = productCodeMapper.coditInforsolidcolSelect();
        List<String> coditInforsuppliersSelect = productCodeMapper.coditInforsuppliersSelect();
        List<String> coditInforStySelect = productCodeMapper.coditInforStySelect();
        List<String> costPriceFinshePropriSelect = productCodeMapper.costPriceFinshePropriSelect();
        List<String> costSellingSalesPricingSelect = productCodeMapper.costSellingSalesPricingSelect();
        List<String> costSellingSalesExpensesLabSelect = productCodeMapper.costSellingSalesExpensesLabSelect();
        List<String> coditAttribTypeOneSelect = productCodeMapper.coditAttribTypeOneSelect();
        List<List<String>> proCode = new ArrayList<>();
        proCode.add(coditTypSelect);
        proCode.add(coditInforSelect);
        proCode.add(coditInforsuppliersSelect);
        proCode.add(coditInforStySelect);
        proCode.add(costPriceFinshePropriSelect);
        proCode.add(costSellingSalesPricingSelect);
        proCode.add(costSellingSalesExpensesLabSelect);
        proCode.add(coditAttribTypeOneSelect);
        return proCode;
    }


}
